package com.aispeech.asr.clib;

import com.aispeech.asr.clib.post.*;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Log
public class PostTerminal {
    public static void main(String[] args) {
        if (args.length < 2) {
            log.warning("[ ERROR ] too few arguments.");
            log.warning("Usage:");
            log.warning("java -cp <xx>.jar com.aispeech.asr.clib.PostTerminal <RES_ROOT> <CUDA_UNIX_PATH>");
            System.exit(1);
        }

        Post.initialize(args[0], args[1]);

        PostEnv env = new PostEnv(false, true, true, true, false, true, false, null, null);

        byte[] in = new byte[64];

        while (true) {
            System.out.println("请输入(q为退出)：");
            try {
                System.in.read(in);
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }

            String line = new String(in).trim();
            log.info(String.format("Recv line: %s", line));

            if (line.startsWith("q")) {
                break;
            } else if (line.startsWith("c")) {
                System.gc();
                log.info("Done System.gc().");
            } else {
                int cnt;
                try {
                    cnt = Integer.parseInt(line);
                } catch (Exception ex) {
                    cnt = 1;
                }

                for (int i = 0; i < cnt; i++) {
                    doPost(env);
//                    AlignInfoStruct.ByReference alignInfoStruct = new AlignInfoStruct.ByReference();
//                    alignInfoStruct.len = 5;
//                    log.info("Done...");
                }
            }
        }

        Post.destroy();
    }

    private static void doPost(PostEnv env) {
        log.info("[ doPost ] begin...");
        AlignInfo alignInfo = new AlignInfo();

        alignInfo.addElement(130, 540, "0", "你好", '\0');
        alignInfo.addElement(970, 1410, "0", "你好", '\0');
        alignInfo.addElement(1410, 2060, "0", "你好", '\0');
        alignInfo.addElement(2860, 3220, "0", "你好", '\0');
        alignInfo.addElement(4180, 4290, "0", "你好", '\0');
//        alignInfo.addElement(4290, 4440, "0", "啊", '\0');
//        alignInfo.addElement(4440, 4500, "0", "啊", '\0');
//        alignInfo.addElement(4530, 6610, "0", "啊", '\0');
//        alignInfo.addElement(6650, 6680, "0", "啊", '\0');
//        alignInfo.addElement(6680, 10880, "0", "啊", '\0');
//        alignInfo.addElement(10910, 11790, "0", "啊", '\0');
//        alignInfo.addElement(12110, 13110, "0", "啊", '\0');
//        alignInfo.addElement(13840, 14010, "0", "啊", '\0');
//        alignInfo.addElement(15590, 15820, "0", "啊", '\0');
//        alignInfo.addElement(16030, 16610, "0", "啊", '\0');
//        alignInfo.addElement(16640, 17380, "0", "啊", '\0');
//        alignInfo.addElement(17410, 17510, "0", "啊", '\0');
        PostResult result = Post.process(alignInfo, env);
        log.info(String.format("[ doPost ] status: %d", result.getStatus()));

        if (result.getStatus() == 0) {
            for (AlignElement alignElement : result.getMerged().getAlignElements()) {
                log.info(String.format("[ doPost ] Merged: %s", alignElement));
            }

//            for (AlignElement alignElement : result.getSegment().getAlignElements()) {
//                log.info(String.format("[ doPost ] Segment: %s", alignElement));
//            }
        }
    }
}
